import { createRouter, createWebHistory } from 'vue-router'
import type { Router } from 'vue-router'

import routers from './modules/index.ts'

// import _mySavedPostion from "./savedPostion"
// _mySavedPostion.init('body')

// vue-router https://router.vuejs.org/
const _router: Router = createRouter({
  history: createWebHistory(),
  strict: true,
  routes: [
    ...routers,
    {
      // 这个匹配所有的路由建议放在添加的路由后面
      path: '/:all(.*)*',
      name: '404',
      component: () => import('@/[...all].vue'),
      meta: {
        title: '找不到页面',
      },
    },
  ],
  scrollBehavior: (to, from, savedPosition) => {
    // _mySavedPostion.saveScrollPostion(from.fullPath, { behavior: 'smooth' })
    if (savedPosition) {
      /**
       * 可以使用_mySavedPostion， 不过需要先初始化, 且为了更好的使用建议配合api请求队列实现
       *  _mySavedPostion.scrollTo(to.fullPath)
       *  return false
       */
      return savedPosition
    }
    return {
      left: 0,
      top: 0,
      behavior: 'smooth',
    }
  },
})
export default _router
